package com.spark.core.transformation

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * sortBy 排序，参数中指定按照什么规则去排序，第二个参数 true/false 指定升序或者降序
 * 无需作用在K,V格式的RDD上
 */
object Demo7_sortBy {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("sortBy")
    conf.setMaster("local")
    val sc = new SparkContext(conf)
    sc.setLogLevel("error")

    val infos = sc.parallelize(Array[(String, Int)](
      ("f", 2),
      ("a", 5),
      ("c", 1),
      ("b", 8))
    )
    // K,V 格式的数据sort
    val result: RDD[(String, Int)] = infos.sortBy(kv => kv._2, false)

    result.foreach(println)

    // 单值也能排序
    val infos1: RDD[Int] = sc.parallelize(Array[Int](400, 200, 500, 100, 300))
    val result1: RDD[Int] = infos1.sortBy(num => num, false)
    result1.foreach(println)

  }
}
